#include "Register.h"

Register::Register(QWidget *parent) : QWidget(parent)
{
    // 关键：启用样式表背景绘制
      setAttribute(Qt::WA_StyledBackground, true);
      setAutoFillBackground(true);
      // 直接设置当前部件的背景色
      setStyleSheet("background-color: #2c3e50;");
      initRegisterInterface();
}
// ==================== 注册界面初始化 ====================
void Register::initRegisterInterface()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 窗口控制按钮区域（右上角）
    QWidget *controlContainer = new QWidget(this);
    controlContainer->setStyleSheet("background:transparent;");
    controlContainer->setFixedHeight(50);
    QHBoxLayout *controlLayout = new QHBoxLayout(controlContainer);
    controlLayout->setContentsMargins(0, 0, 10, 0);
    controlLayout->setAlignment(Qt::AlignRight | Qt::AlignTop);

    QPushButton *btnMinimize = new QPushButton("−", controlContainer);
    btnMinimize->setFixedSize(30, 30);
    btnMinimize->setStyleSheet("QPushButton { background: transparent; color: white; border: none; font-size: 20px; font-weight: bold; }"
                               "QPushButton:hover { background: #5dade2; border-radius: 3px; }");
    connect(btnMinimize, &QPushButton::clicked, [this](){
        if (QMainWindow *mainWin = qobject_cast<QMainWindow*>(parentWidget()->topLevelWidget())) {
            mainWin->showMinimized();
        }
    });
    QPushButton *btnMaximize = new QPushButton("□", controlContainer);
    btnMaximize->setFixedSize(30, 30);
    btnMaximize->setStyleSheet("QPushButton { background: transparent; color: white; border: none; font-size: 16px; }"
                               "QPushButton:hover { background: #5dade2; border-radius: 3px; }");
    connect(btnMaximize, &QPushButton::clicked, [this, btnMaximize]() {
        QMainWindow *mainWin = qobject_cast<QMainWindow*>(parentWidget()->topLevelWidget());
        if(!mainWin)return;
        if (mainWin->isMaximized()) {
            mainWin->showNormal();
            btnMaximize->setText("□");
        } else {
            mainWin->showMaximized();
            btnMaximize->setText("◫");
        }
    });

    QPushButton *btnClose = new QPushButton("×", controlContainer);
    btnClose->setFixedSize(30, 30);
    btnClose->setStyleSheet("QPushButton { background: transparent; color: white; border: none; font-size: 20px; font-weight: bold; }"
                            "QPushButton:hover { background: #e74c3c; border-radius: 3px; }");
    connect(btnClose, &QPushButton::clicked, [this](){
        if (QMainWindow *mainWin = qobject_cast<QMainWindow*>(parentWidget()->topLevelWidget())) {
            mainWin->close();
        }
    });
    controlLayout->addWidget(btnMinimize);
    controlLayout->addWidget(btnMaximize);
    controlLayout->addWidget(btnClose);

    // 注册表单容器（居中）
    QWidget *formContainer = new QWidget(this);
    formContainer->setStyleSheet("background:transparent;");
    QVBoxLayout *formLayout = new QVBoxLayout(formContainer);
    formLayout->setSpacing(25);
    formLayout->setContentsMargins(100, 30, 100, 60);

    // 注册标题
    m_labRegisterTitle = new QLabel("用户注册", formContainer);
    m_labRegisterTitle->setStyleSheet("color:white; font-size:32px; font-weight:bold; margin-bottom: 20px;");
    m_labRegisterTitle->setAlignment(Qt::AlignCenter);

    // 用户名输入行
    QWidget *userWidget = new QWidget(formContainer);
    QHBoxLayout *userLayout = new QHBoxLayout(userWidget);
    userLayout->setSpacing(15);
    userLayout->setContentsMargins(0, 0, 0, 0);

    m_labRegisterUser = new QLabel("用户名：", userWidget);
    m_labRegisterUser->setStyleSheet("color:white; font-size:18px; min-width: 90px;");
    m_labRegisterUser->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_edtRegisterUser = new QLineEdit(userWidget);
    m_edtRegisterUser->setStyleSheet("background:white; color:#333; font-size:16px; padding:12px; border:2px solid #bdc3c7; border-radius:8px;"
                                     "QLineEdit:focus { border-color: #3498db; }");
    m_edtRegisterUser->setMinimumHeight(45);
    m_edtRegisterUser->setPlaceholderText("请设置用户名（3-15位）");
    m_edtRegisterUser->setStyleSheet("background:white;");

    userLayout->addWidget(m_labRegisterUser);
    userLayout->addWidget(m_edtRegisterUser, 1);

    // 密码输入行
    QWidget *pwdWidget = new QWidget(formContainer);
    QHBoxLayout *pwdLayout = new QHBoxLayout(pwdWidget);
    pwdLayout->setSpacing(15);
    pwdLayout->setContentsMargins(0, 0, 0, 0);

    m_labRegisterPwd = new QLabel("密码：", pwdWidget);
    m_labRegisterPwd->setStyleSheet("color:white; font-size:18px; min-width: 90px;");
    m_labRegisterPwd->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_edtRegisterPwd = new QLineEdit(pwdWidget);
    m_edtRegisterPwd->setEchoMode(QLineEdit::Password);
    m_edtRegisterPwd->setStyleSheet("background:white; color:#333; font-size:16px; padding:12px; border:2px solid #bdc3c7; border-radius:8px;"
                                    "QLineEdit:focus { border-color: #3498db; }");
    m_edtRegisterPwd->setMinimumHeight(45);
    m_edtRegisterPwd->setPlaceholderText("请设置密码（6-20位）");
    m_edtRegisterPwd->setStyleSheet("background:white;");

    pwdLayout->addWidget(m_labRegisterPwd);
    pwdLayout->addWidget(m_edtRegisterPwd, 1);

    // 确认密码输入行
    QWidget *confirmPwdWidget = new QWidget(formContainer);
    QHBoxLayout *confirmPwdLayout = new QHBoxLayout(confirmPwdWidget);
    confirmPwdLayout->setSpacing(15);
    confirmPwdLayout->setContentsMargins(0, 0, 0, 0);

    m_labRegisterConfirmPwd = new QLabel("确认密码：", confirmPwdWidget);
    m_labRegisterConfirmPwd->setStyleSheet("color:white; font-size:18px; min-width: 90px;");
    m_labRegisterConfirmPwd->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_edtRegisterConfirmPwd = new QLineEdit(confirmPwdWidget);
    m_edtRegisterConfirmPwd->setEchoMode(QLineEdit::Password);
    m_edtRegisterConfirmPwd->setStyleSheet("background:white; color:#333; font-size:16px; padding:12px; border:2px solid #bdc3c7; border-radius:8px;"
                                           "QLineEdit:focus { border-color: #3498db; }");
    m_edtRegisterConfirmPwd->setMinimumHeight(45);
    m_edtRegisterConfirmPwd->setPlaceholderText("请再次输入密码");
    m_edtRegisterConfirmPwd->setStyleSheet("background:white;");

    confirmPwdLayout->addWidget(m_labRegisterConfirmPwd);
    confirmPwdLayout->addWidget(m_edtRegisterConfirmPwd, 1);

    // 按钮区
    QWidget *btnWidget = new QWidget(formContainer);
    QHBoxLayout *btnLayout = new QHBoxLayout(btnWidget);
    btnLayout->setSpacing(20);
    btnLayout->setContentsMargins(0, 20, 0, 0);
    btnLayout->setAlignment(Qt::AlignCenter);

    m_btnRegister = new QPushButton("注册", btnWidget);
    m_btnRegister->setStyleSheet("QPushButton {"
                                 "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #3498db, stop:1 #2980b9);"
                                 "color:white; font-size:18px; font-weight:bold; padding:12px 40px;"
                                 "border:none; border-radius:8px; min-width: 120px;"
                                 "}"
                                 "QPushButton:hover { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #2980b9, stop:1 #3498db); }"
                                 "QPushButton:pressed { background: #21618c; }");
    m_btnRegister->setFixedHeight(50);

    m_btnToLogin = new QPushButton("返回登录", btnWidget);
    m_btnToLogin->setStyleSheet("QPushButton {"
                                "background: #ecf0f1; color:#2c3e50; font-size:18px; font-weight:bold; padding:12px 30px;"
                                "border:2px solid #bdc3c7; border-radius:8px; min-width: 120px;"
                                "}"
                                "QPushButton:hover { background: #d5dbdb; border-color: #95a5a6; }"
                                "QPushButton:pressed { background: #bfc9ca; }");
    m_btnToLogin->setFixedHeight(50);

    btnLayout->addWidget(m_btnRegister);
    btnLayout->addWidget(m_btnToLogin);

    // 组装表单
    formLayout->addStretch(1);
    formLayout->addWidget(m_labRegisterTitle);
    formLayout->addWidget(userWidget);
    formLayout->addWidget(pwdWidget);
    formLayout->addWidget(confirmPwdWidget);
    formLayout->addWidget(btnWidget);
    formLayout->addStretch(2);

    // 组装主布局
    mainLayout->addWidget(controlContainer);
    mainLayout->addWidget(formContainer, 1);

    connect(m_btnRegister,&QPushButton::clicked,this,&Register::on_btnRegister_clicked);
    connect(m_btnToLogin,&QPushButton::clicked,this,&Register::on_btnToLogin_clicked);
}


void Register::on_btnRegister_clicked()
{
    if (!checkRegisterInput()) return;

    QString username = m_edtRegisterUser->text().trimmed();
    QString password = m_edtRegisterPwd->text().trimmed();

    emit registerRequested(username,password);
    emit registerSuccess(username);
}

void Register::on_btnToLogin_clicked()
{
    // 发送切换到登录界面的信号
    emit switchToLogin();
    // 清空输入框
    m_edtRegisterUser->clear();
    m_edtRegisterPwd->clear();
    m_edtRegisterConfirmPwd->clear();
}

bool Register::checkRegisterInput()
{
    QString username = m_edtRegisterUser->text().trimmed();
    QString password = m_edtRegisterPwd->text().trimmed();
    QString confirmPwd = m_edtRegisterConfirmPwd->text().trimmed();

    if (username.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请设置用户名！");
        m_edtRegisterUser->setFocus();
        return false;
    }
    if (username.length() < 3 || username.length() > 15) {
        QMessageBox::warning(this, "输入错误", "用户名长度需在3-15位之间！");
        m_edtRegisterUser->setFocus();
        return false;
    }
    if (password.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请设置密码！");
        m_edtRegisterPwd->setFocus();
        return false;
    }
    if (password.length() < 6 || password.length() > 20) {
        QMessageBox::warning(this, "输入错误", "密码长度需在6-20位之间！");
        m_edtRegisterPwd->setFocus();
        return false;
    }
    if (confirmPwd != password) {
        QMessageBox::warning(this, "输入错误", "两次输入的密码不一致！");
        m_edtRegisterConfirmPwd->setFocus();
        return false;
    }
    return true;
}
